#import "scene_menu.h"

@implementation SceneMenu

- (id)initSceneMenu
{
	if ((self = [super init]))
	{
		[[CCDirector sharedDirector] setProjection:CCDirectorProjection2D];

		/*set global handles
		*/
		g_SceneMenu = self;

		/*music
		*/
		MusicLoad();
		MusicPlayMain();
		[self schedule:@selector(tickMusic:) interval:BGM_MAIN_TIME + BGM_TIME_SPLITE];
	}
	return self;
}

- (void)tickMusic:(ccTime)Time
{
	MusicPlayMain();
}

- (void)tickSceneChangeToComic:(ccTime)Time
{
	scene_dummy *_SceneDummy;

	[self unschedule:@selector(tickSceneChangeToComic:)];

	_SceneDummy = [[scene_dummy alloc] initSceneDummy:SCENE_CHANGE_TO_COMIC];
	[[CCDirector sharedDirector] replaceScene:_SceneDummy];
	[_SceneDummy release];
}

- (void)tickSceneChangeToLibrary:(ccTime)Time
{
	scene_dummy *_SceneDummy;

	[self unschedule:@selector(tickSceneChangeToLibrary:)];

	_SceneDummy = [[scene_dummy alloc] initSceneDummy:SCENE_CHANGE_TO_LIBRARY];
	[[CCDirector sharedDirector] replaceScene:_SceneDummy];
	[_SceneDummy release];
}

- (void)setState:(int)State
{
	switch (State)
	{
	case SCENE_MENU_STATE_MAIN:
		[self freeLayer];
		g_LayerMenuMain = [[layer_menu_main alloc] initLayerMenuMain];
		[self addChild:g_LayerMenuMain];
		[g_LayerMenuMain release];
		g_LayerMenuMain.positionInPixels = CGPointZero;
		break;
	case SCENE_MENU_STATE_CHAPTER:
		[self freeLayer];
		g_LayerMenuChapter = [[layer_menu_chapter alloc] initLayerMenuChapter];
		[self addChild:g_LayerMenuChapter];
		[g_LayerMenuChapter release];
		g_LayerMenuChapter.positionInPixels = CGPointZero;
		break;
	default:;
	}
}

- (void)freeLayer
{
	if (g_LayerMenuMain != nil)
	{
		[self removeChild:g_LayerMenuMain cleanup:TRUE];
		g_LayerMenuMain = nil;
	}
	if (g_LayerMenuChapter != nil)
	{
		[self removeChild:g_LayerMenuChapter cleanup:TRUE];
		g_LayerMenuChapter = nil;
	}
	if (g_LayerMenuOption != nil)
	{
		[self removeChild:g_LayerMenuOption cleanup:TRUE];
		g_LayerMenuOption = nil;
	}
}

- (void)sceneChangeToComic
{
	sprite_dummy *_Loading;

	/*music
	*/
	MusicStop();
	[self unschedule:@selector(tickMusic:)];

	/*free layers
	*/
	[self freeLayer];

	/*flag
	*/
	g_ChpaterComicPrev = true;
	g_ChapterNextMissionID = -1;

	/*loading image
	*/
	_Loading = [[sprite_dummy alloc] initSpriteDummy:true];
	[self addChild:_Loading];
	[_Loading release];
	_Loading.positionInPixels = g_ScrrenCenter;

	/*tick
	*/
	[self schedule:@selector(tickSceneChangeToComic:) interval:1.0f];
}

- (void)sceneChangeToLibrary
{
	sprite_dummy *_Loading;

	/*music
	*/
	MusicStop();
	[self unschedule:@selector(tickMusic:)];

	/*free layers
	*/
	[self freeLayer];

	/*loading image
	*/
	_Loading = [[sprite_dummy alloc] initSpriteDummy:true];
	[self addChild:_Loading];
	[_Loading release];
	_Loading.positionInPixels = g_ScrrenCenter;

	/*tick
	*/
	[self schedule:@selector(tickSceneChangeToLibrary:) interval:1.0f];
}

@end